//
//  YLCDentistDynamicView.m
//  LinYa
//
//  Created by 初程程 on 2018/1/25.
//  Copyright © 2018年 初程程. All rights reserved.
//

#import "YLCDentistDynamicView.h"
#import "YLCDynamicCell.h"
#import "YLCQuestionModel.h"
#define DYNAMIC_CELL @"YLCDynamicCell"
@interface YLCDentistDynamicView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@end
@implementation YLCDentistDynamicView
{
    NSMutableDictionary *cellDic;

    UICollectionView *dynamicTable;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        cellDic = [NSMutableDictionary dictionary];
        self.backgroundColor = [UIColor whiteColor];
        self.selectSignal = [RACSubject subject];
        self.reloadSignal = [RACSubject subject];
        [self createCustomView];
    }
    return self;
}
- (void)createCustomView{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    
    flow.minimumLineSpacing = 10;
    
    dynamicTable = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
    
    dynamicTable.backgroundColor = YLC_COMMON_BACKCOLOR;
    
    [dynamicTable registerClass:[YLCDynamicCell class] forCellWithReuseIdentifier:DYNAMIC_CELL];
    
    dynamicTable.delegate = self;
    
    dynamicTable.dataSource = self;
    
    [dynamicTable setupNormalRefreshHeadAndFootWithTarget:self headAction:@selector(reloadData) footAction:@selector(didTriggerLoadNextPageData)];
    
    [dynamicTable setupOnlyHeadRefrshWithTarget:self headAction:@selector(reloadData)];
    
    [self addSubview:dynamicTable];
}
- (void)beganRefresh{
    [dynamicTable.mj_header beginRefreshing];
}
- (void)setDentistData:(NSArray *)dentistData{
    _dentistData = dentistData;
    
    if (JK_IS_ARRAY_NIL(dentistData)) {
        YLCEmptyView *view = [[YLCEmptyView alloc] init];
        
        [dynamicTable showEmptyViewWithEmptyView:view];
    }
    
    [dynamicTable reloadData];
    
    [dynamicTable.mj_header endRefreshing];
}
- (void)resetNoMore{
    [dynamicTable.mj_footer endRefreshing];
}
- (void)loadedAllData{
    [dynamicTable.mj_footer endRefreshingWithNoMoreData];
}
- (void)reloadData{
    [self.reloadSignal sendNext:@"0"];
}
- (void)didTriggerLoadNextPageData{
    
    NSInteger page = [_dentistData count]/[PAGE_LIMIT integerValue];
    
    [self.reloadSignal sendNext:[NSString stringWithFormat:@"%ld",page]];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dentistData.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSString *identifier = [cellDic objectForKey:[NSString stringWithFormat:@"%@", indexPath]];
    if (identifier == nil) {
        identifier = [NSString stringWithFormat:@"YLCDynamicCell%@", [NSString stringWithFormat:@"%@", indexPath]];
        [cellDic setValue:identifier forKey:[NSString stringWithFormat:@"%@", indexPath]];
        [dynamicTable registerClass:[YLCDynamicCell class]  forCellWithReuseIdentifier:identifier];
    }
    
    YLCDynamicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    if (indexPath.row<_dentistData.count) {
        YLCQuestionModel *model = _dentistData[indexPath.row];
        
        if (JK_IS_ARRAY_NIL(model.thumbList)) {
            cell.photoList = model.imglist;
            cell.imgList = model.imglist;
        }else{
            cell.photoList = model.thumbList;
            cell.imgList = model.imglist;
        }
        
        cell.type = _type;
        
        cell.model = model;
        
        if (_type == 1) {
            cell.deleteBtn.hidden = NO;
            cell.deleteBtn.tag = indexPath.row;
            [cell.deleteBtn addTarget:self action:@selector(deleteInfo:) forControlEvents:UIControlEventTouchUpInside];
        }else{
            cell.deleteBtn.hidden = YES;
        }
    }
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    YLCQuestionModel *model = _dentistData[indexPath.row];
    
    return CGSizeMake(self.frameWidth, model.height+60);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.selectSignal sendNext:indexPath];
}
#pragma mark -- 我的帖子删除
- (void)deleteInfo:(UIButton*)button{
    YLCQuestionModel *model = _dentistData[button.tag];
    YLCAccountModel *userModel = [[YLCAccountManager shareManager] currentAccount];
    [YLCHUDShow showSelectAlertWithTarget:self Message:@"是否确认删除" leftTitle:@"确认" rightTitle:@"取消" selector:^(NSInteger index) {
        if (index==0) {
            NSDictionary *parms = @{@"userId":userModel.user_id,
                                    @"token":userModel.token,
                                    @"forumId":model.id};
            [YLCNetWorking loadNetServiceWithParam:parms method:@"POST" urlPath:@"app/forum/del" delegate:self response:^(id responseObject, NSError *error) {
                if (error) {
                    return ;
                }
                [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                
                NSMutableArray *container = [_dentistData mutableCopy];
                
                [container removeObject:model];
                
                _dentistData = [container copy];
                
                [dynamicTable reloadData];
            }];
        }
    }];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [dynamicTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsZero);
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
